package com.york.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.york.eduservice.entity.EduSubject;
import com.york.eduservice.entity.subject.OneSubject;
import com.york.eduservice.entity.subject.TwoSubject;
import com.york.eduservice.excel.SubjectData;
import com.york.eduservice.listener.SubjectExcelListener;
import com.york.eduservice.mapper.EduSubjectMapper;
import com.york.eduservice.service.EduSubjectService;
import com.york.utils.exception.BusinessException;
import com.york.utils.exception.result.ResponseEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:课程科目 服务实现类
 * @Data: 2022/3/7 15:39
 * @Author: yangboxiong
 * @Version
 * @since JDK 1.8
 */
@Service
@Slf4j
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {
    /**
     * 添加课程分类
     *
     * @param file
     */
    @Override
    public void saveSubject(MultipartFile file, EduSubjectService subjectService) {
        try {
            //文件输入流
            InputStream input = file.getInputStream();
            //调用方法进行读取
            EasyExcel.read(input, SubjectData.class, new SubjectExcelListener(subjectService)).sheet().doRead();
        } catch (Exception e) {
            throw new BusinessException(ResponseEnum.UPLOAD_ERROR, e);
        }
    }

    /**
     * 课程分类的列表功能（树形）
     *
     * @return
     */
    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //查询所有一级分类
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id", "0");
        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapperOne);
        //查询所有二级分类
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id", "0");
        List<EduSubject> twoSubjectList = baseMapper.selectList(wrapperTwo);

        //创建list集合，用于存储最终封装数据
        List<OneSubject> finalSubjectList = new ArrayList<>();
        //封装一级分类
        //查询出来的一级分类list集合遍历，得到每一个一级分类的对象，获取每一个一级分类的对象的值
        //封装到要求的list集合里面list<OneSubject> finalSubjectList = new ArrayList<>();
        //遍历一级分类集合
        for (int i = 0; i < oneSubjectList.size(); i++) {
            EduSubject eduSubject = oneSubjectList.get(i);
            //把eduSubject里面的值获取出来，放到oneSubjectList对象里面

            OneSubject oneSubject = new OneSubject();
            BeanUtils.copyProperties(eduSubject, oneSubject);
            //多个oneSubjectList放在finalSubjectList里面
            finalSubjectList.add(oneSubject);

            //在一级分类循环遍历查询所有的二级分类
            List<TwoSubject> twoFinalSubjectList = new ArrayList<>();
            //遍历二级分类list集合
            for (int m = 0; m < twoSubjectList.size(); m++) {
                //获取每个二级分类
                EduSubject tSubject = twoSubjectList.get(m);
                //判断二级分类parentid和一级分类ids是否一样
                if (tSubject.getParentId().equals(eduSubject.getId())) {
                    //把twoSubject的值复制到TwoSubject里面，放到twoSubjectList里面
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(tSubject, twoSubject);
                    twoFinalSubjectList.add(twoSubject);
                }
            }
            //把一级下面所有二级分类放到一级分类里面
            oneSubject.setChildren(twoFinalSubjectList);
        }
        //封装二级分类
        return finalSubjectList;
    }

}
